package com.cliff.joinme;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager.MulticastLock;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.androidquery.AQuery;
import com.cliff.joinme.toolkit.ControlPointManager;
import com.cliff.joinme.widget.AmazingListView;

public class MainActivity extends SelectRendererActivity implements
		OnItemClickListener {

	protected ControlPointManager jcp = ControlPointManager.getInstance();
	protected AQuery aq = new AQuery(this);
	AmazingListView lsComposer;
	SectionComposerAdapter adapter;
	MulticastLock multicastLock;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		lsComposer = (AmazingListView) findViewById(R.id.lsComposer);
		lsComposer.setPinnedHeaderView(LayoutInflater.from(this).inflate(
				R.layout.item_composer_header, lsComposer, false));
		lsComposer.setAdapter(adapter = new SectionComposerAdapter(this));
		lsComposer.setOnItemClickListener(this);

		aq.id(R.id.imageViewWirelessRouter).clicked(this, "updateWifiInfo");
		updateWifiInfo();
		acquireMulticastLock();
		jcp.startControlPoint();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		jcp.stopControlPoint();
		releaseMulticastLock();
		super.onDestroy();
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		new AlertDialog.Builder(this)
				.setIcon(android.R.drawable.ic_dialog_alert)
				.setTitle(R.string.quit)
				.setMessage(R.string.really_quit)
				.setPositiveButton(R.string.yes,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								// Stop the activity
								MainActivity.this.finish();
							}
						}).setNegativeButton(R.string.no, null).show();
		// super.onBackPressed();
	}

	public void updateWifiInfo() {
		aq.id(R.id.TextViewWifiInfo).text(getWifiInfo());
	}

	protected String getWifiInfo() {

		String ssid = jcp.getSSIDName(this);
		String ipaddr = jcp.getLocalIpAddress();

		if (null != ssid) {
			if (null != ipaddr)
				return ssid + " : " + ipaddr;
			else
				return ssid + " : NO IP";
		} else if (null != ipaddr) {
			return "ETHERNET" + " : " + ipaddr;
		} else
			return "NO NETWORK";
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
//		MenuItem item = menu.add(Constants.GROUP_NONE, Constants.ITEM_SETTINGS,
//				1, R.string.menu_settings);
//		item.setIcon(android.R.drawable.ic_menu_manage);
//		item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
//
//		return true;
		updateRendererDisplay();
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub

		switch (position) {
		case 0:
			startBrowseImageActivity();
			break;
		case 1:
			startBrowseMusicActivity();
			break;
		case 2:
			startBrowseVideoActivity();
			break;
		default:
			Toast.makeText(this, "Clicked item=" + position + "; id=" + id,
					Toast.LENGTH_SHORT).show();
			break;
		}
	}

	protected void startBrowseImageActivity() {
		startActivity(new Intent(MainActivity.this, BrowseImageActivity.class));
	}

	protected void startBrowseMusicActivity() {
		startActivity(new Intent(MainActivity.this, BrowseMusicActivity.class));
	}

	protected void startBrowseVideoActivity() {
		startActivity(new Intent(MainActivity.this, BrowseVideoActivity.class));
	}

	protected void acquireMulticastLock() {
		WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
		multicastLock = wifiManager.createMulticastLock("multicast.test");
		multicastLock.acquire();
	}

	protected void releaseMulticastLock() {
		multicastLock.release();
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);
		if (selectRendererSucceeded)		
			updateRendererDisplay();
		
		return true;
	}
	
}
